package com.rx.sample.controller;

import com.rx.sample.model.Address;
import com.rx.sample.model.Balance;
import com.rx.sample.model.User;
import com.rx.sample.service.AddressService;
import com.rx.sample.service.BalanceService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AccountController {

    @Autowired
    private BalanceService balanceService;
    @Reference
    private AddressService addressService;

    private static final Map<Integer, User> userMap = new HashMap() {{
        put(1, User.builder().id(1).name("张三").build());
        put(2, User.builder().id(2).name("李四").build());
        put(3, User.builder().id(3).name("王五").build());
    }};

    @RequestMapping("/account/user")
    public User getUser(@RequestParam Integer id) {
        if (id != null && userMap.containsKey(id)) {
            User user = userMap.get(id);
            Balance balance = balanceService.getBalance(id);
            user.setBalance(balance);
            Address address = addressService.getAddress(id);
            user.setAddress(address);
            return user;
        }
        return User.builder().build();
    }

}
